/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.web.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.User;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.AuthorizationManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.PermissionManager;
import com.genia.toolbox.projects.toolbox_basics_project.web.context.UserWebContext;

/**
 * Tag to display body if user is not authorized to see contents.
 */
@SuppressWarnings("serial")
public final class SpringIsNotAuthorizedTag
    extends TagSupport
{

  /**
   * Permission to check.
   */
  private String permissionDescription;

  /**
   * the {@link PermissionManager} to use.
   */
  private PermissionManager permissionManager;

  /**
   * userWebContext to determine if user is authorized.
   */
  private UserWebContext userWebContext;

  /**
   * Used to check of user is authorized.
   */
  private AuthorizationManager authorizationManager;



  /**
   * getter for the permissionDescription property.
   * 
   * @return the permissionDescription
   */
  public String getPermissionDescription()
  {
    return permissionDescription;
  }



  /**
   * setter for the permissionDescription property.
   * 
   * @param permissionDescription
   *          the permissionDescription to set
   */
  public void setPermissionDescription(String permissionDescription)
  {
    this.permissionDescription = permissionDescription;
  }



  /**
   * getter for the authorizationManager property.
   * 
   * @return the authorizationManager
   */
  public AuthorizationManager getAuthorizationManager()
  {
    return authorizationManager;
  }



  /**
   * setter for the authorizationManager property.
   * 
   * @param authorizationManager
   *          the authorizationManager to set
   */
  public void setAuthorizationManager(AuthorizationManager authorizationManager)
  {
    this.authorizationManager = authorizationManager;
  }



  /**
   * getter for the userWebcontext property.
   * 
   * @return the userWebcontext
   */
  public UserWebContext getUserWebContext()
  {
    return userWebContext;
  }



  /**
   * setter for the userWebcontext property.
   * 
   * @param userWebcontext
   *          the userWebcontext to set
   */
  public void setUserWebContext(UserWebContext userWebcontext)
  {
    this.userWebContext = userWebcontext;
  }



  /**
   * Processing of the start tag.
   * 
   * @return SKIP_BODY
   * @throws JspException
   *           if an exception occured
   */
  @Override
  public int doStartTag()
      throws JspException
  {

    try {

      User currentUser = getUserWebContext().getCurrentUser();
      if (currentUser != null && !getAuthorizationManager().isAuthorized(currentUser, getPermissionManager().getPermission(getPermissionDescription()))) {
        return EVAL_BODY_INCLUDE;
      }

    }
    catch (TechnicalException te) {
      throw new JspException(te);
    }

    return SKIP_BODY;
  }



  /**
   * Release state.
   */
  @Override
  public void release()
  {
    super.release();

  }



  /**
   * getter for the permissionManager property.
   * 
   * @return the permissionManager
   */
  public PermissionManager getPermissionManager()
  {
    return permissionManager;
  }



  /**
   * setter for the permissionManager property.
   * 
   * @param permissionManager
   *          the permissionManager to set
   */
  public void setPermissionManager(PermissionManager permissionManager)
  {
    this.permissionManager = permissionManager;
  }

};
